<template>
	<view v-if="xShow==true">
		<view class="top bg-white">
			<view class="status_bar"></view>
			<u-tabs :list="oneMenu" :current="xid" @change="yemx" style=""></u-tabs>
		</view>	
		<view class="u-p-l-30 u-p-r-30" style="margin-top: 160upx;">
			<view class="bg-white margin-top radius" v-for="(xitem,xindex) in yemxList" :key="xindex" @click="goOrderDetails(xitem._id)">	
				<view class=" " style="width:100%;">
					<view class="u-p-20 bg-white u-m-t-30" style="border-radius: 20upx;">
						<view class=" u-flex " style="color: #393D4A;">
							<view class="u-font-34" style="font-weight: bold;padding-left: 5px;"> {{xitem.title}}</view>
						</view>
						<view class="u-flex u-col-top u-content-color u-p-t-20 u-p-l-10">
							<image class="u-flex-2" v-if="xitem.cover" :src="xitem.cover" style="width: 300upx;height: 150upx;margin-right: 10upx;border-radius: 10upx;"></image>
							<view class="u-flex-3" :class="xitem.cover?'threeline':'twoline'" style="line-height: 22px;">{{xitem.description}}</view>
						</view>	
						<view class="u-flex u-row-between u-light-color u-p-t-20">
							<view class=""> {{xitem.author||'云庭金服官方'}}<uni-dateformat class="u-p-l-20" :date="xitem.inTime*1000" :format="xitem.year==year?'M月dd日 hh:mm':'yyyy月M月dd日 hh:mm'"></uni-dateformat></view>
							
							<view class="u-flex">
								<u-icon name="eye"></u-icon><view class="u-font-10 u-p-l-10" v-text="xitem.click"></view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-empty class="u-p-30" text="暂无案件" mode="list" v-if="yemxList.length==0 && status != 'loading'"></u-empty>
			<u-loadmore class="u-p-30" :status="status" :load-text="{
				loadmore: '轻轻上拉',
				loading: '努力加载中',
				nomore: '我也是有底线的'
			}" v-else=""/>
		</view>
		<app-update ref="app_update" :force="true" :tabbar="false"></app-update>
		<u-tabbar :list="vuex_tabbar" :mid-button="true" :before-switch="beforeSwitch" inactive-color="#cecece" active-color="#2979ff"></u-tabbar>
	</view>
</template>
<script>
let timestamp = Date.parse(new Date());
let date = new Date(timestamp);
	
import uniDateformat from 'components/uni-dateformat/uni-dateformat.vue';
import ksjPreviewImage from 'components/kd-preview-image/kd-preview-image.vue';
import appUpdate from "@/components/yzhua006-update/app-update.vue" // 版本检测升级
	export default {
		components: {
			uniDateformat,ksjPreviewImage,appUpdate
		},
		//#ifdef MP-WEIXIN
		onShareAppMessage(res) {//分享
			try {//从本地缓存中同步获取指定 key 对应的内容。
				var mv = uni.getStorageSync('userinfo');
				if (res.from === 'button') {// 来自页面内分享按钮
				 console.log(res.target)
				}
				// return {
				//  title: xv.store.stores_name,
				//  path: '/pages/home/index?member_openid='+mv.openid+"&stores_id="+xv.store.stores_id
				// }
			} catch (e) {
				// error
			}
			
		 },
		//#endif
		data() {
			return {
				xShow:false,
				preview:false,
				preview_imgs:[],
				preview_index:0,
				xid:0,
				oneMenu:[
					{
						'name':'默认',
						'orderby':'sort desc'
					},
					{
						'name':'最新',
						'orderby':'upTime desc'
					},
					{
						'name':'热门',
						'orderby':'click desc'
					}
				],
				list_data:{
					state:'',
					name:'',
					limit:10,
					orderby:'inTime desc',
					page:1
				},
				yemxList:[],
				login:false,
				submit_data:{
					user_id:''
				},
				year:date.getFullYear(),
				upgradeType: 'pkg', //pkg 整包 wgt 升级包
				upgradeContent: '', //更新内容
				upgradeUrl: '', //更新地址
			}
		},
		onShow:function(){//返回时接收子页面的传参
			try {
				const intervalID = uni.getStorageSync('intervalID');
				clearInterval(intervalID);
				if(this.$refs.app_update)this.$refs.app_update.update();  //调用子组件 检查更新
			} catch (e) {
				// error
			}
		},
		onLoad:function(data){
			uni.showLoading({
			  title: '加载中'
			});
			try {//从本地缓存中同步获取指定 key 对应的内容。
				const value = uni.getStorageSync('userinfo');
				if (value) {
					this.list_data.user_id = value['_id'];
					this.submit_data.user_id = value['_id'];
				}
			} catch (e) {
				// error
			}
			this.xid = data.state?parseInt(data.state):0;
			this.list_data.state = this.xid;
			this.getConfig(data);
			this.getList()
		},
		onPullDownRefresh() {
			this.yemxList = [];
			this.page = 0;
			this.limit = 30;
			this.getList()
		},
		onReachBottom() {
			this.getList('add');
		},
		methods: {
			beforeSwitch(index) {
				// 只能切换偶数项
				if(index == 1){
					uni.scanCode({
						success: function(res) {
							uni.navigateTo({
								url: "/pages/center/authorized?content=" + res.result
							});
							return;
						}
					});
				}else{
					return true;
				};
			},
			//返回上一页
			goback(){
				uni.navigateBack({
					delta:1
				})
			},
			previewfn(imgs,index){
				this.preview_imgs=imgs;
				this.preview_index = index;
				this.preview=true
			},
			getConfig(index){
				this.$u.api.getOrderIndex(this.submit_data).then(res=>{
					
				})
			},
			yemx(index){
				this.index = [];
				this.page = 0;
				this.limit = 30;
				this.xid =index;
				this.list_data.orderby = this.oneMenu[index].orderby;
				this.getList();
			},
			goOrderDetails(_id){//跳转案件详情
				uni.navigateTo({
					url:"/pages/article/details?_id="+_id
				})
			},
			getList(action = 'refresh'){
				this.status = 'loading';
				if(action=='add'){
					this.list_data.page = this.list_data.page+1
				}else{
					this.yemxList = [];
					this.list_data.page=1
				}
				//获取信息
				// var pdata = url.getSignStr(this.list_data);
				this.$u.api.getArticleList(this.list_data).then(res=>{
					uni.stopPullDownRefresh();
					this.xShow = true;
					uni.hideLoading();
					//this.yemxList = res.data.order_info;
					var max_page = res.total/this.limit;
					if(max_page<=this.page){
						this.status = 'nomore';
						return; 
					}
					var datax = []
					if(res.code==1){
						datax = res.data;
					}					
					if (action === 'refresh') {
						this.yemxList = [];
					}	
					
					datax.forEach(item => {		
						let date = new Date(item.inTime*1000);
						item.year = date.getFullYear()					
						this.yemxList.push(item);
					});
					this.page = this.page+1;
					this.status = 'nomore';
				})
			},
			/*
			* 检测版本升级
			*/
			checkVersionClick() {
				// 获取版本信息
				console.log(plus)
				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					var platform = uni.getSystemInfoSync().platform
					//根据当前平台去后台检测版本号
					//这是我的逻辑，可自定义
					var pdata = {
						version: widgetInfo.version,
						name: widgetInfo.name //没啥用
					}
					this.$u.api.checkVersionClick(pdata).then(res=>{
						uni.hideLoading();
						if(res.code == 1){
					
					console.log(res.data)
							if (res.data.pkgUrl != '' && res.data.wgtUrl == '') {
								this.upgradeType = 'pkg'
								this.upgradeContent = res.data.content
								this.upgradeUrl = res.data.pkgUrl
								this.$refs.yomolUpgrade.show()
							} else {
								this.upgradeType = 'wgt'
								this.upgradeContent = res.data.content
								this.upgradeUrl = res.data.wgtUrl
								this.$refs.yomolUpgrade.show()
							}
							return false;
						}
					}).catch(res=>{ 
						return console.error(res);
					});
					// uni.request({
					// 	url: 'http://jdy.varuuu.com/api/version/check/app',
					// 	data: {
					// 		version: widgetInfo.version,
					// 		name: widgetInfo.name //没啥用
					// 	},
					// 	success: (res) => {
					// 		console.log(res);
					// 		if (res.data.code == 1 && res.data.update) {
					// 			//我的后台会返回一个安装包地址（pkgUrl），升级包地址（wgtUrl）
					// 			//判空来判断是非是整包升级还是资源包升级
					// 			//content是版本更新内容 \n换行
					// 			//ios用户pkgUrl是苹果市场App地址，android自定义下载地址
					// 			if (res.data.pkgUrl != '' && res.data.wgtUrl == '') {
					// 				this.upgradeType = 'pkg'
					// 				this.upgradeContent = res.data.content
					// 				this.upgradeUrl = res.data.pkgUrl
					// 				this.$refs.yomolUpgrade.show()
					// 			} else {
					// 				this.upgradeType = 'wgt'
					// 				this.upgradeContent = res.data.content
					// 				this.upgradeUrl = res.data.wgtUrl
					// 				this.$refs.yomolUpgrade.show()
					// 			}
					// 		} 
					// 	},
					// 	fail: (error) => {
					// 		console.log(error);
					// 	}
					// });
				});
			},
		}
	}
</script>
<style  lang="scss" scoped>
	.line-bottom-short{
		color: $u-type-primary;
	}
</style>
